<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Server{
    private $socket;
    private $url = null;
    private $port = null;
    private $connected = false;
    private $encrypted = true;
    private $name = null;

    public function connect($url, $port){
        $this->url = $url;
        $this->port = $port;
        $this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
        $this->connected = socket_connect($this->socket,$this->url,$this->port);
    }
    
    public function checkAvailability(){
        if($this->connected){
            $request = "availability\r\nHost servermanager.sfsu.edu\r\n";
            socket_write($this->socket, $request, strlen($request));
            $response = socket_read($this->socket, 1024);
            return $response;
        }else{
            return "Not connected to server\r\n";
        }
    }
    public function getStatus(){
        if($this->connected){
            $request = "system-status\r\nHost servermanager.sfsu.edu\r\n";
            socket_write($this->socket, $request, strlen($request));
            $response = socket_read($this->socket, 1024);
            return $response;
        }else{
            return "Not connected to server\r\n";
        }
    }
    public function createJob(){
        if($this->connected){
            $request = "create-job\r\nHost servermanager.sfsu.edu\r\n";
            //echo $request;
            $file = file_get_contents("./msroll.xml");
            $request .= "Content Length ".  filesize($file)."\r\n";
            $request .= "Media Type job/".filetype($file)."\r\n\r\n";
            $request .= $file;
            socket_write($this->socket,$request,strlen($request));
            $response = socket_read($this->socket, 1024);
            return $response;
        }else{
            return "Not connected to server\r\n";
        }
    }
    private function encrypt($data){
        $key = $this->hex2bin("fedcba9876543210");
        $td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");
        mcrypt_generic_init($td, $key, CIPHER_IV);
        $encrypted = mcrypt_generic($td, $data);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $encryp = bin2hex($encrypted);
    }
    private function hex2bin($hexdata) {
      $bindata = "";

      for ($i = 0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
      }

      return $bindata;
    }
    public function close(){
        socket_close($this->socket);
        $this->connected = false;
    }
}
?>
